package com.joey.cloud.flowable.listener;

import com.alibaba.fastjson.JSONObject;
import com.joey.cloud.flowable.event.MyFlowMsgEvent;
import com.joey.cloud.flowable.service.FlowEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 监听自定义发送消息事件
 */
@Service
@Slf4j
public class MyFlowMsgEventListener implements ApplicationListener<MyFlowMsgEvent> {
    @Resource
    FlowEventService flowEventService;

    @Override
    public void onApplicationEvent(MyFlowMsgEvent event) {
        JSONObject json = event.getEventJson();
        String eventType = event.getEventType();
        log.info("触发流程监控spring事件",eventType);
        //发送消息
        if(eventType.startsWith("TASK_")){
            JSONObject task = json.getJSONObject("entity");
            flowEventService.buildEventMsg(eventType,json.getString("processInstanceId"),task.getString("id"),task.getString("taskDefinitionKey"));
        }else if(eventType.startsWith("PROCESS_")){
            flowEventService.buildEventMsg(eventType,json.getString("processInstanceId"),null,null);
        }
    }
}
